package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.order.OrderForm;
import com.cskaoyan.gateway.form.order.OrderListForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.Data;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @Author lirui
 * @Date 2021/12/13 16:08
 * @describe:
 */


@RestController
@RequestMapping("/shopping")
public class OrderController {
      @Reference(timeout = 3000,check = false)
      private OrderCoreService orderCoreService;

      @Reference(timeout = 3000,check = false)
      private OrderQueryService orderQueryService;




    /**
    *@Description:
    * 创建订单
    */
    @PostMapping("/order")
    public ResponseData order(@RequestBody CreateOrderRequest request , HttpServletRequest servletRequest){
        String userInfo= (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);

        //设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());

        CreateOrderResponse response = orderCoreService.createOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return  new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
        


    }

    /**
    *@Description:
    *获取当前模块用户所有订单
    */

    @GetMapping("/order")
    public ResponseData queryOrderList(OrderListRequest orderListRequest,HttpServletRequest servletRequest){
        String userInfo= (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        orderListRequest.setUserId(uid);
        
        OrderListResponse response=orderQueryService.queryOrder(orderListRequest);
        OrderListForm orderListForm = new OrderListForm();
        orderListForm.setData(response.getDetailInfoList());
        orderListForm.setTotal(response.getTotal());
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return  new ResponseUtil<>().setData(orderListForm);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 获取订单详情
     * @return
     */
    @RequestMapping(value = "/order/{id}",method = RequestMethod.GET)
    public ResponseData getOrederDetail(@PathVariable String id,HttpServletRequest servletRequest){
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        String username = (String) object.get("username");
        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);

        OrderDetailDtoResponse response= orderQueryService.queryOrderDetail(request,uid,username);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            ResponseData<Object> responseData = new ResponseData<>();
            responseData.setCode(200);
            responseData.setMessage("success");
            responseData.setSuccess(true);
            responseData.setResult(response.getResult());
            responseData.setTimestamp(System.currentTimeMillis());
            return responseData;
        }

        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 删除订单
     */
    @RequestMapping(value = "/order/{id}",method = RequestMethod.DELETE)
    public ResponseData deleteOrder(@PathVariable String id,HttpServletRequest servletRequest){
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        if (userInfo == null){
            return new ResponseUtil<>().setErrorMsg(500,OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        int affectRows = orderCoreService.deleteOrder(id);
        if (affectRows == 1){
            ResponseData<Object> responseData = new ResponseData<>();
            responseData.setCode(200);
            responseData.setMessage("success");
            responseData.setSuccess(true);
            OrderForm responseSmallData = new OrderForm();
            responseSmallData.setCode("000000");
            responseSmallData.setMsg("成功");
            responseData.setResult(responseSmallData);
            return responseData;
        }else {
            return new ResponseUtil<>().setErrorMsg(500,OrderRetCode.DB_EXCEPTION.getMessage());
        }
    }

    /**
     *取消订单
     */
    @RequestMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map, HttpServletRequest servletRequest){
        String orderId = (String) map.get("orderId");
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        if (userInfo == null){
            return new ResponseUtil<>().setErrorMsg(500,OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        /**
         * 修改订单状态为7
         */
        int affectRows = orderCoreService.cancelOrder(orderId);
        ResponseData<Object> responseData = new ResponseData<>();
        responseData.setSuccess(true);
        responseData.setMessage("success");
        responseData.setCode(200);
        responseData.setResult("成功");
        return responseData;
    }
}
